class Solution {
public:
    string removeKdigits(string num, int k) {
        vector<char> st;
        int n = num.size() - k;
        for(char i : num){
            while(k > 0 && !st.empty() && st.back() > i){
                // cout << "-" << st.top() << endl;
                st.pop_back();
                k --;
            }
            st.push_back(i);
           // cout << "+" << st.top() << endl;
        }
        for(;k > 0;k--){
            st.pop_back();
        }
        string ans;
        bool firstzero = true;
        for(auto& c : st){
            if(firstzero == true && c =='0'){
                continue;
            }
            firstzero = false;
            ans += c;
        }
        // reverse(ans.begin(),ans.end());
        return ans == "" ? "0" : ans;
    }
};
class Solution {
public:
    vector<int> mostCompetitive(vector<int>& nums, int k) {
        vector<int> st;
        k = nums.size() - k;
        for(int i : nums){
            while(k > 0 && !st.empty()  && st.back() > i){
                st.pop_back();
                k--;
            }
            st.push_back(i);
        }
        while(k > 0){
            st.pop_back(); k--;
        }
        return st;
    }
};